home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Various / DevDisk 65 (1989)(DevWare PD).zip / DevDisk 65 (1989)(DevWare PD).adf / starsrc / stars.c < prev    next >
C/C++ Source or Header  |  1990-07-11  |  2KB  |  141 lines

  1. /*  :ts=8
  2.  * stars.c:  An attmept to fight boredom (yet again).
  3.  * by Leo L. Schwab   8606.30
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <intuition/intuition.h>
  8.  
  9. #define NSTARS        64
  10.  
  11. extern void *OpenLibrary(), *OpenScreen(), *OpenWindow(), *GetMsg();
  12. extern short rnd();
  13.  
  14. long *IntuitionBase, *GfxBase;
  15.  
  16. struct NewScreen scrdef = {
  17.     0, 0, 320, 200,
  18.     4,        /*  # planes  */
  19.     -1, -1,
  20.     NULL,
  21.     CUSTOMSCREEN,
  22.     NULL, NULL, NULL, NULL
  23. };
  24.  
  25. struct NewWindow windef = {
  26.     0, 0, 320, 200,
  27.     -1, -1,
  28.     CLOSEWINDOW,
  29.     WINDOWCLOSE,
  30.     NULL, NULL, NULL, NULL, NULL,
  31.     0, 0, 0, 0,
  32.     CUSTOMSCREEN
  33. };
  34.  
  35. struct Window *win;
  36. struct Screen *scr;
  37. struct RastPort *rp;
  38. short x[NSTARS], y[NSTARS], z[NSTARS];
  39. short xo[NSTARS], yo[NSTARS];
  40.  
  41.  
  42. main (ac, av)
  43. char *av[];
  44. int ac;
  45. {
  46.     long xs, ys;
  47.     long *msg;
  48.     short magic;
  49.     register short i, inc;
  50.  
  51.     if (ac > 1)
  52.         magic = atoi (av[1]);
  53.     else
  54.         magic = 256;
  55.  
  56.     if (ac > 2)
  57.         inc = atoi (av[2]);
  58.     else
  59.         inc = 3;
  60.  
  61.     openstuff ();
  62.     rnd (-5286);
  63.     SetRast (rp, 0L);
  64.     for (xs=0; xs<16; xs++)
  65.         SetRGB4 (&(scr -> ViewPort), xs, xs, xs, xs);
  66.  
  67.     for (i=0; i<NSTARS; i++)
  68.         mkpoint (i);
  69.  
  70.     FOREVER {
  71.         for (i=0; i<NSTARS; i++) {
  72.             if ((z[i] -= inc) <= 0)
  73.                 mkpoint (i);
  74.             xs = x[i] * magic / z[i] + 160;
  75.             ys = y[i] * magic / z[i] + 100;
  76.             SetAPen (rp, 0L);
  77.             WritePixel (rp, (long) xo[i], (long) yo[i]);
  78.             if (xs < 0 || xs > 319 || ys < 0 || ys > 199)
  79.                 mkpoint (i);
  80.             else {
  81.                 SetAPen (rp, (long) (256-z[i] >> 4));
  82.                 WritePixel (rp, xs, ys);
  83.                 xo[i] = xs;  yo[i] = ys;
  84.             }
  85.         }
  86.         if (msg = GetMsg (win -> UserPort)) {
  87.             ReplyMsg (msg);
  88.             break;
  89.         }
  90.     }
  91.  
  92.     closestuff ();
  93. }
  94.  
  95. mkpoint (i)
  96. register short i;
  97. {
  98.     x[i] = rnd (256) - 128;
  99.     y[i] = rnd (150) - 75;
  100.     z[i] = 255;
  101. }
  102.  
  103. openstuff ()
  104. {
  105.     if (!(IntuitionBase = OpenLibrary ("intuition.library", 0L))) {
  106.         printf ("Intuition open failed.\n");
  107.         die ();
  108.     }
  109.  
  110.     if (!(GfxBase = OpenLibrary ("graphics.library", 0L))) {
  111.         printf ("graphics open failed.\n");
  112.         die ();
  113.     }
  114.  
  115.     if (!(scr = OpenScreen (&scrdef))) {
  116.         printf ("Can't open screen.\n");
  117.         die ();
  118.     }
  119.  
  120.     windef.Screen = scr;
  121.     if (!(win = OpenWindow (&windef))) {
  122.         printf ("Window painted shut.\n");
  123.         die ();
  124.     }
  125.     rp = &(scr -> RastPort);
  126. }
  127.  
  128. closestuff ()
  129. {
  130.     if (win)        CloseWindow  (win);
  131.     if (scr)        CloseScreen  (scr);
  132.     if (GfxBase)        CloseLibrary (GfxBase);
  133.     if (IntuitionBase)    CloseLibrary (IntuitionBase);
  134. }
  135.  
  136. die ()
  137. {
  138.     closestuff ();
  139.     exit (-1);
  140. }
  141.